using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Scenario_Editor.WR2TerrainLoader
{
    internal class CollisionBlock
    {
        List<CollisionData> collisionData;
        
        private struct CollisionData
        {
            short blockID;
            List<ushort> polygons;

            public static CollisionData FromStream(BinaryReader br, short length)
            {
                CollisionData data = new CollisionData();
                data.polygons = new List<ushort>();

                data.blockID = (short)br.ReadUInt16();
                for (int i = 0; i < (int)length; i++)
                {
                    data.polygons.Add(br.ReadUInt16());
                }

                return data;
            }

            public int SizeInBytes
            {
                get
                {
                    return 4 + this.polygons.Count * 2;
                }
            }

            public short BlockID { get { return blockID; } set { blockID = value; } }

            internal void Save(BinaryWriter bw)
            {
                bw.Write((ushort)this.polygons.Count);
                bw.Write(this.blockID);
                for (int i = 0; i < this.polygons.Count; i++)
                {
                    bw.Write(this.polygons[i]);
                }

            }
        }

        public CollisionBlock()
        {
            this.collisionData = new List<CollisionData>();
        //    this.material = material;
        //    this.values = values;
        }
        
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("entries: " + this.collisionData.Count.ToString() + " Block: ");
            for (int i = 0; i < this.collisionData.Count; i++)
			{
                sb.Append(" " + collisionData[i].BlockID.ToString());
			}
            return sb.ToString();
        }

        internal static List<CollisionBlock> FromStream(System.IO.BinaryReader br, uint collisionLength, PointerTable pointerTable)
        {
            List<CollisionBlock> collisionsBlocks = new List<CollisionBlock>();
            for (int i = 0; i < pointerTable.OffsetsCompletePosition.Count; i++)
            {
                CollisionBlock block = new CollisionBlock();
                short length = 0;
                do
                {
                    length = br.ReadInt16();
                    if (length > 0)
                    {
                        CollisionData data = CollisionBlock.CollisionData.FromStream(br, length);
                        block.collisionData.Add(data);
                    }
                } while (length > 0);
                collisionsBlocks.Add(block);
            } 
            return collisionsBlocks;
        }

        internal void Save(System.IO.BinaryWriter bw)
        {
            for (int i = 0; i < this.collisionData.Count; i++)
            {
                this.collisionData[i].Save(bw);
            }
            //Terminater byte;
            bw.Write((ushort)0);
        }
        public int SizeInBytes
        {
            get
            {
                int size = 2; //this includes the 00 at the end
                for (int i = 0; i < this.collisionData.Count; i++)
                {
                    size += this.collisionData[i].SizeInBytes;
                }
                return size;
            }
        }


    }

}
